﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class PatientDetailsForm : Form
    {

        private Patient patient;
        List<string> diagNames;

        public PatientDetailsForm(Patient patient2)
        {
            InitializeComponent();
            patient = patient2;
            nameTextBox.Text = patient.patName;
            surnameTextBox.Text = patient.surname;
            patronymicTextBox.Text = patient.patronymic;
            dateTimePicker1.Value = patient.dob;
            diagNames = DiagnoseDB.GetDiagnoseNames(patient);
           // diagNames = DiagnoseDB.DiagNames(patient.patDiagnose);
            foreach (string el in diagNames)
            {
                diagListBox.Items.Add(el);
            }

        }

        private void diagListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Diagnose> diagList = DiagnoseDB.GetDiagnoses();
            string dName = diagListBox.SelectedItem.ToString();
            foreach (Diagnose el in diagList)
            {
                if (el.name == dName)
                {
                    diagDescrTextBox.Text = el.description;
                }
            }
        }


        private void RemoveButt_Click(object sender, EventArgs e)
        {
            /*  /* int diagDel = diagListBox.SelectedIndex;
               diagListBox.Items.RemoveAt(diagListBox.SelectedIndex);*/
            List<Diagnose> diagList = DiagnoseDB.GetDiagnoses();
            string dName = diagListBox.SelectedItem.ToString();
            int? idToDel = -1;
            foreach (Diagnose el in diagList)
            {
                if (el.name == dName)
                {
                    idToDel = el.ID;
                }
            }
            Patient pat = new Patient();
            //PatientDetailForm patDet = new PatientDetailForm(pat);
            // List<int> patDiagsDel = pat.patDiagnose;
            foreach (int el in pat.patDiagnose)
            {
                if (el == idToDel)
                {
                    pat.patDiagnose.Remove(el);
                }
            }
            PatientDB.EditedPatientToDB(pat);
            MessageBox.Show("Changes saved!");
            // PatientForm.SetDataToGrid();*/
        }

       private void OKButt_Click_1(object sender, EventArgs e)
        {
            patient.patName = nameTextBox.Text;
            patient.surname = surnameTextBox.Text;
            patient.patronymic = patronymicTextBox.Text;
            patient.dob = dateTimePicker1.Value;
            List<string> selectedList = new List<string>();
            foreach (var item in diagListBox.Items)
            {
                selectedList.Add(item.ToString());
            }
            patient.patDiagnose = DiagnoseDB.GetDiagnoseID(selectedList);
            DialogResult = DialogResult.OK;
            Close();
        }

       /* private void OKButt_Click_1(object sender, EventArgs e)
        {
            patient.patName = nameTextBox.Text;
            patient.surname = surnameTextBox.Text;
            patient.patronymic = patronymicTextBox.Text;
            patient.dob = dateTimePicker1.Value;
           // List<string> selectedList = new List<string>();
            foreach (var item in diagListBox.Items)
            {
                diagNames.Add(item.ToString());
            }
            patient.patDiagnose = DiagnoseDB.GetDiagnoseID(diagNames);
            DialogResult = DialogResult.OK;
            Close();
        }*/
        private void AddButt_Click_1(object sender, EventArgs e)
        {
            List<Diagnose> diagList = DiagnoseDB.GetDiagnoses();
            SelectDiag selDiag = new SelectDiag(diagList);
            if (selDiag.ShowDialog() == DialogResult.OK)
            {
               if (patient.patDiagnose != null)
                {
                    if (!patient.patDiagnose.Contains(selDiag.GetSelectedItem().ID.Value))   ////////////////
                    {
                        //patient.patDiagnose.Add(selDiag.GetSelectedItem().ID.Value);
                        diagListBox.Items.Add(selDiag.GetSelectedItem().name);
                    }
                }
                else
                {
                    diagListBox.Items.Add(selDiag.GetSelectedItem().name);
                   
                }
              
               
            }
        }
        /* private void AddButt_Click(object sender, EventArgs e)
         {

             List<Diagnose> diagList = DiagnoseDB.GetDiagnoses();
             SelectDiag selDiag = new SelectDiag(diagList);
             if (selDiag.ShowDialog() == DialogResult.OK)
             {
                 if (!patient.patDiagnose.Contains(selDiag.GetSelectedItem().ID.Value))
                 {
                     patient.patDiagnose.Add(selDiag.GetSelectedItem().ID.Value);
                 }
             }
         }*/


        private void Cancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void RemoveButt_Click_1(object sender, EventArgs e)
        {
            diagListBox.Items.RemoveAt(diagListBox.SelectedIndex);
        }

        private void diagDescrTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void diagListBox_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            List<Diagnose> diagList = DiagnoseDB.GetDiagnoses();
           string dName = (string)diagListBox.SelectedItem;
            //string dName = diagListBox.SelectedItem.ToString();
            foreach (Diagnose el in diagList)
            {
                if (el.name == dName)
                {
                    diagDescrTextBox.Text = el.description;
                }
            }
        }
    }
}
